/**
 * 
 */
package org.ycsoft.metroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.FilenameFilter;

import org.ycsoft.MetroidReader.MetroidMetadata;
import org.ycsoft.MetroidReader.MetroidReader;

/**
 * @author Cherif YAYA
 * 
 */
public class MetroidMap extends ListActivity {

	//public constant
	
	
	public List<String> mapFiles = new ArrayList<String>();
	public List<String> displayNames = new ArrayList<String>();

	private int numberMaps = 0; // number of maps

	

	// Contains all the maps in the data directory
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.maps);
		fillMein();
		Button btnClose = (Button)findViewById(R.id.btnClose);
		btnClose.setOnClickListener(closeListener);
	}
	
	private OnClickListener closeListener = new OnClickListener(){
		public void onClick(View v){
			//close activity
			finish();
		}
	};

	public void fillMein() {
		try {
			SharedPreferences settings = getSharedPreferences(
		    		  Commons.PREFS_NAME, 0);
			String rep = settings.getString(Commons.PREFS_FOLDER, Commons.PREFS_DEF_FOLDER);
			File dir = new File(rep);
			// String[] files=dir.list();
			File[] files = dir.listFiles(filtre);
			if (files == null || files.length == 0) {
				numberMaps = 0;
				displayNames.add(getResources().getString(R.string.no_maps)); // test if package exists
			} else {
				numberMaps = 0;
				for (File map : files) {
					MetroidReader reader = new MetroidReader(map.getAbsolutePath());
					reader.loadIndexes(false);
					//MetroidReader reader=new MetroidReader(rep+map.getName());
					MetroidMetadata meta = reader.getMetadata();
					reader.closeStream();
					String toShow = meta.getCity() + " , " + meta.getMean();
					displayNames.add(toShow);
					mapFiles.add(map.getAbsolutePath());
					numberMaps++;
				}
			}

		} catch (Exception ioe) {
			ioe.printStackTrace();
			bip(ioe.getMessage());
		}

		ArrayAdapter<String> items = new ArrayAdapter<String>(this,
				R.layout.listbold, displayNames);
		setListAdapter(items);
	}

	/*
	 * Filter for metroid files (.mtd extensions)
	 */
	FilenameFilter filtre = new FilenameFilter() {

		public boolean accept(File dir, String filename) {
			return filename.endsWith(".mtd");
		}
	};

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if (numberMaps == 0) {
			// no map
			return;
		}
		Intent i = new Intent(this, MetroidCentral.class);
		i.putExtra(Commons.KEY_MAPFILE, mapFiles.get(position));
		i.putExtra(Commons.KEY_MAPNAME, displayNames.get(position).split(",")[0]);
		startActivity(i);
		//changer la map par defaut
		SharedPreferences settings = getSharedPreferences(
	    		  Commons.PREFS_NAME, 0);
	      SharedPreferences.Editor editor = settings.edit();
	      editor.putString(Commons.PREFS_CITY,
	    		  displayNames.get(position).split(",")[0]);
	      editor.putString(Commons.PREFS_MAPFILE,
	    		  mapFiles.get(position));
	      editor.commit();
	      
	}

	public void bip(String p) {
		//showAlert("Done?",R.drawable.icon, p, "ok", false);
		new AlertDialog.Builder(MetroidMap.this)
		.setTitle("Done?")
		.setMessage(p)
		.setIcon(R.drawable.icon)
		.setPositiveButton("ok",
				new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {}})
		.show();
	}
}
